package br.com.caiocarrara.goodbuy.controller;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caiocarrara.goodbuy.dao.ProductDao;
import br.com.caiocarrara.goodbuy.model.Cart;
import br.com.caiocarrara.goodbuy.model.Item;

@Resource
public class CartController {
	private final Cart cart;
	private final ProductDao productDao;
	private final Result result;
	
	public CartController(Cart cart, ProductDao productDao, Result result) {
		this.cart = cart;
		this.productDao = productDao;
		this.result = result;
	}
	
	@Get("/cart")
	public void view() {
		
	}
	
	@Post("/cart")
	public void add(Item item) {
		productDao.reload(item.getProduct());
		this.cart.add(item);
		
		result.redirectTo(ProductsController.class).list();
	}
	
	@Delete("/cart/{itemIndex}")
	public void remove(int itemIndex) {
		this.cart.remove(itemIndex);
		result.redirectTo(this).view();
	}
}
